package GUI;



import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import main.Declarations;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import strategies.PluginsAPI;

import Data.HibernateUtil;
import Data.Tag;


import executor.CreateUserTagExecutor;
import executor.ExecutorsHash;
import executor.SearchStrategyTagExecutor;
import executor.SearchUserTagExecutor;
import executor.TagFileByUserTag;




/**
 * The GUI of The Program
 * 
 * @author Vladimir Sarkisyan & Erez Levy
 * 
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DropBoxGUI2 implements Runnable{

	private Display display;
	private Shell shell;
	private Combo UserTagCombo4Taging;
	private Combo combo4tagFile;
	private List listOfFiles;
	private Button openFile;
	private Button button5;
	private Combo addStrategyCombo;
	private Text addStrategy;

	private Text text3;
	private Combo combo1;
	private Combo combo2;
	private Combo combo3;
	private Button button4;
	private Text text7;
	private Text text6;
	private Text createUserTag;
	private Text searchF;
	private Button button3;
	private Text text5;
	private Text text4;
	private Button button2;
	private Text text2;
	private Button button1;
	private static HashMap<String, ArrayList<String>> strategiesHash;
	
	private static DropBoxGUI2 instance = new DropBoxGUI2();
	
	//constructor
	private DropBoxGUI2(){
		strategiesHash = new HashMap<String, ArrayList<String>>();	
	}
	
	
	//get the DropBoxGUI2 view
	public static DropBoxGUI2 getInstance() {
		return instance;
	}
	
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		//create the form of the view
		initGUI();
		
		//add 
		listenersInitialization ();
		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
		    // If no more entries in the event queue
		        display.sleep();
		      }
		}
	}
	
	
	/**
	 * initialize the components of the GUI  - list, butons ...
	 * 
	 */
	private void initGUI() {
	try {
		display = new Display();
		shell = new Shell(display);
		shell.setText("Dropbox");
		shell.setLayout(new GridLayout(3, false));
		
		FormLayout thisLayout = new FormLayout();
		shell.setLayout(thisLayout);
		shell.setSize(706, 504);
		{
			FormData combo4tagFileLData = new FormData();
			combo4tagFileLData.top =  new FormAttachment(0, 1000, 110);
			combo4tagFileLData.width = 90;
			combo4tagFileLData.height = 21;
			combo4tagFileLData.right =  new FormAttachment(1000, 1000, -95);
			combo4tagFile = new Combo(shell, SWT.NONE);
			combo4tagFile.setLayoutData(combo4tagFileLData);
		}
		{
			FormData listOfFilesLData = new FormData();
			listOfFilesLData.left =  new FormAttachment(0, 1000, 29);
			listOfFilesLData.top =  new FormAttachment(0, 1000, 23);
			listOfFilesLData.width = 421;
			listOfFilesLData.height = 435;
			listOfFilesLData.bottom =  new FormAttachment(1000, 1000, -12);
			listOfFilesLData.right =  new FormAttachment(1000, 1000, -245);
			listOfFiles = new List(shell, SWT.V_SCROLL | SWT.BORDER);
			listOfFiles.setLayoutData(listOfFilesLData);
		}
		{
			openFile = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData openFileLData = new FormData();
			openFileLData.top =  new FormAttachment(0, 1000, 35);
			openFileLData.width = 98;
			openFileLData.height = 44;
			openFileLData.right =  new FormAttachment(1000, 1000, -68);
			openFile.setLayoutData(openFileLData);
			openFile.setText("Open File");
		}

		{
			button5 = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData button5LData = new FormData();
			button5LData.top =  new FormAttachment(0, 1000, 422);
			button5LData.width = 43;
			button5LData.height = 21;
			button5LData.right =  new FormAttachment(1000, 1000, -16);
			button5.setLayoutData(button5LData);
			button5.setText("add");
		}
		{
			FormData addStrategyComboLData = new FormData();
			addStrategyComboLData.top =  new FormAttachment(0, 1000, 422);
			addStrategyComboLData.width = 114;
			addStrategyComboLData.height = 21;
			addStrategyComboLData.right =  new FormAttachment(1000, 1000, -71);
			addStrategyCombo = new Combo(shell, SWT.NONE);
			addStrategyCombo.setLayoutData(addStrategyComboLData);
			//for debug 
			addStrategyCombo.add("TagBySize");
			addStrategyCombo.add("TagByType");
			addStrategyCombo.add("TagByLastModified");
		}
		{
			addStrategy = new Text(shell, SWT.READ_ONLY);
			FormData addStrategyLData = new FormData();
			addStrategyLData.top =  new FormAttachment(0, 1000, 404);
			addStrategyLData.width = 85;
			addStrategyLData.height = 13;
			addStrategyLData.right =  new FormAttachment(1000, 1000, -123);
			addStrategy.setLayoutData(addStrategyLData);
			addStrategy.setText("Choose Strategy:");
		}

		{
			button4 = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData button4LData = new FormData();
			button4LData.top =  new FormAttachment(0, 1000, 370);
			button4LData.width = 43;
			button4LData.height = 22;
			button4LData.right =  new FormAttachment(1000, 1000, -16);
			button4.setLayoutData(button4LData);
			button4.setText("create");
		}
		{
			text7 = new Text(shell, SWT.BORDER);
			FormData text7LData = new FormData();
			text7LData.top =  new FormAttachment(0, 1000, 370);
			text7LData.width = 131;
			text7LData.height = 16;
			text7LData.right =  new FormAttachment(1000, 1000, -71);
			text7.setLayoutData(text7LData);
			text7.setText("");
		}
		{
			text6 = new Text(shell, SWT.READ_ONLY);
			FormData text6LData = new FormData();
			text6LData.top =  new FormAttachment(0, 1000, 352);
			text6LData.width = 81;
			text6LData.height = 18;
			text6LData.right =  new FormAttachment(1000, 1000, -127);
			text6.setLayoutData(text6LData);
			text6.setText("Create a tag:");
		}
		{
			createUserTag = new Text(shell, SWT.READ_ONLY);
			FormData createUserTagLData = new FormData();
			createUserTagLData.top =  new FormAttachment(0, 1000, 327);
			createUserTagLData.width = 103;
			createUserTagLData.height = 19;
			createUserTagLData.right =  new FormAttachment(1000, 1000, -51);
			createUserTag.setLayoutData(createUserTagLData);
			createUserTag.setText("Tags creation");
			createUserTag.setFont(new Font(display, "Courier New", 10, SWT.BOLD));
		}
		{
			FormData searchFLData = new FormData();
			searchFLData.top =  new FormAttachment(0, 1000, 151);
			searchFLData.width = 100;
			searchFLData.height = 20;
			searchFLData.right =  new FormAttachment(1000, 1000, -51);
			searchF = new Text(shell,SWT.READ_ONLY);
			searchF.setLayoutData(searchFLData);
			searchF.setText("Search files");
			searchF.setFont(new Font(display, "Courier New", 10, SWT.BOLD));
		}
		{
			button3 = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData button3LData = new FormData();
			button3LData.top =  new FormAttachment(0, 1000, 236);
			button3LData.width = 49;
			button3LData.height = 62;
			button3LData.right =  new FormAttachment(1000, 1000, -17);
			button3.setLayoutData(button3LData);
			button3.setText("search");
		}
		{
			combo3 = new Combo(shell, SWT.NONE);
			FormData combo3LData = new FormData();
			combo3LData.top =  new FormAttachment(0, 1000, 277);
			combo3LData.width = 91;
			combo3LData.height = 21;
			combo3LData.right =  new FormAttachment(1000, 1000, -94);
			combo3.setLayoutData(combo3LData);
			combo3.setText("");
		}
		{
			combo2 = new Combo(shell, SWT.NONE);
			FormData combo2LData = new FormData();
			combo2LData.top =  new FormAttachment(0, 1000, 236);
			combo2LData.width = 91;
			combo2LData.height = 21;
			combo2LData.right =  new FormAttachment(1000, 1000, -94);
			combo2.setLayoutData(combo2LData);
			combo2.setText("");
		}
		{
			text5 = new Text(shell, SWT.READ_ONLY);
			FormData text5LData = new FormData();
			text5LData.top =  new FormAttachment(0, 1000, 262);
			text5LData.width = 70;
			text5LData.height = 16;
			text5LData.right =  new FormAttachment(1000, 1000, -138);
			text5.setLayoutData(text5LData);
			text5.setText("Choose tag:");
		}
		{
			text4 = new Text(shell, SWT.READ_ONLY);
			FormData text4LData = new FormData();
			text4LData.top =  new FormAttachment(0, 1000, 220);
			text4LData.width = 87;
			text4LData.height = 16;
			text4LData.right =  new FormAttachment(1000, 1000, -121);
			text4.setLayoutData(text4LData);
			text4.setText("By Strategy:");
		}
		{
			button2 = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData button2LData = new FormData();
			button2LData.top =  new FormAttachment(0, 1000, 185);
			button2LData.width = 50;
			button2LData.height = 23;
			button2LData.right =  new FormAttachment(1000, 1000, -17);
			button2.setLayoutData(button2LData);
			button2.setText("search");
		}
		{
			combo1 = new Combo(shell, SWT.NONE);
			FormData combo1LData = new FormData();
			combo1LData.top =  new FormAttachment(0, 1000, 187);
			combo1LData.width = 91;
			combo1LData.height = 21;
			combo1LData.right =  new FormAttachment(1000, 1000, -94);
			combo1.setLayoutData(combo1LData);
			combo1.add("all the files");
			//combo1.setText("combo1");
		}
		{
			text3 = new Text(shell, SWT.READ_ONLY);
			FormData text3LData = new FormData();
			text3LData.top =  new FormAttachment(0, 1000, 171);
			text3LData.width = 118;
			text3LData.height = 16;
			text3LData.right =  new FormAttachment(1000, 1000, -90);
			text3.setLayoutData(text3LData);
			text3.setText("By your tags:");
		}
		{
			text2 = new Text(shell, SWT.READ_ONLY);
			text2.setText("Tag the file");
			FormData text2LData = new FormData();
			text2LData.width = 97;
			text2LData.height = 19;
			text2LData.top =  new FormAttachment(0, 1000, 91);
			text2LData.right =  new FormAttachment(1000, 1000, -111);
			text2.setLayoutData(text2LData);
		}
		{
			button1 = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData button1LData = new FormData();
			button1LData.top =  new FormAttachment(0, 1000, 110);
			button1LData.width = 50;
			button1LData.height = 23;
			button1LData.right =  new FormAttachment(1000, 1000, -17);
			button1.setLayoutData(button1LData);
			button1.setText("tag");
		}
	
		}//end of try 
		catch (Exception e) {
			e.printStackTrace();
		}
	
	}//end of initGUI();
	
	
	
	/**
	 * Initialize the listeners for the GUI components
	 * 
	 */
	private void listenersInitialization (){
		
	    Listener butListener = new Listener() {

			@Override
			public void handleEvent(Event event) {
				String line, line2;
				
				CreateUserTagExecutor createUserTagExecutor;
				SearchUserTagExecutor searchUserTagExecutor;
				SearchStrategyTagExecutor searchStrategyTagExecutor;
				
				//the button search for the - User Tag in search file				
				if(event.widget == button1){
					//take the tag name from the combo
					line = combo4tagFile.getText();
					//combo4tagFile.setText("");
					//for debug
					System.out.println("tag: "+ line);
					//
					
					//and take the item from the list
					int count = listOfFiles.getItemCount();
					
					//check there items in the list and combo was not empty 					
					if((count > 0)&&(!line.equals(""))){
						//get the file name that checked
						int index = listOfFiles.getFocusIndex();
						String fileName = listOfFiles.getItem(index);
						
						TagFileByUserTag tagFileByUserTag = (TagFileByUserTag) ExecutorsHash.Instance.getExecutor("TagFileByUserTag");
						//for debug
						System.out.println("tag: "+ line);
						System.out.println("fileName: "+ fileName);
						//
						
						//set the parameters
						tagFileByUserTag.setFileName(fileName);
						tagFileByUserTag.setUserTag(line);
					
						tagFileByUserTag.execute();
					}				
				}//end of event.widget == button1
				
				//if the search button for choose a user tag was pushed
				else if(event.widget == button2){
					line = combo1.getText();
					combo1.setText("");
					
					//if the line in the combo is not empty
					if(!line.equals("")){
						searchUserTagExecutor = (SearchUserTagExecutor) ExecutorsHash.Instance.getExecutor("SearchUserTag");
						//set the name of the tag
						searchUserTagExecutor.setTag(line);
						searchUserTagExecutor.execute();
					}	
				}
				
				//if the search button for choose a strategy&Tag  was pushed
				else if(event.widget == button3){
					line = combo2.getText();
					combo2.setText("");	
					
					line2 = combo3.getText();
					combo3.setText("");
					
					//check that the lines was not empty
					if((line != null)&&(line2 != null)){
						searchStrategyTagExecutor = (SearchStrategyTagExecutor) ExecutorsHash.Instance.getExecutor("SearchStrategyTag");
						
						searchStrategyTagExecutor.setStrategy(line);
						searchStrategyTagExecutor.setTag(line2);
						
						searchStrategyTagExecutor.execute();
					}				
				}
				//end of event = button 3 - the strategy search
				
				//if the search button for create user- insert name Tag  was pushed
				else if(event.widget == button4){
					line = text7.getText();
					text7.setText("");
						
					//check that the tag not empty
					if(!line.equals("")){				
						createUserTagExecutor = (CreateUserTagExecutor) ExecutorsHash.Instance.getExecutor("CreateUserTag");
						//set the tag and execute
						createUserTagExecutor.setTag(line);
						createUserTagExecutor.execute();
						//add to the combo for tag files and the combo for search
						combo4tagFile.add(line);
						combo1.add(line);
					}										
				}
				
				//in case it is button open file
				else if(event.widget == openFile){
					//check if the list not empty
					if(listOfFiles.getItemCount() > 0){
						//the index of the file that checked
						int index= listOfFiles.getFocusIndex();
						String fileName = listOfFiles.getItem(index);
						
						//create a full path
						String strPath = Declarations.instance.getPath();
						strPath = strPath + "\\" + fileName;
						//create the file
						File file = new File(strPath);
						
						//check if the file exists
						if(file.exists()){
							
						}
					}
				}//end of open file button event
				
				//button for add strategy
				else if(event.widget == button5){
					String strategy = addStrategyCombo.getText();
					//if it is not empty
					if(!strategy.equals("")){
						//add to the combo for search
						//combo2.add(strategy);
						
						//add the strategy and his list of tag to the hashMap
						ArrayList<String> tagList;
						tagList = PluginsAPI.instance.getStrategyTags(strategy);
						
						if(strategiesHash.get(strategy)==null){
							combo2.add(strategy);
							strategiesHash.put(strategy, tagList);
						}

						//add the strategy to the PluginsAPI list
						//PluginsAPI.instance.addStrategyToList(strategy);
						
						//tag all the files
						PluginsAPI.instance.tagAllTheFiles(strategy);
						
						//add the strategy to the list in the PluginsAPI
						//PluginsAPI.instance.addStrategyToList(strategy);
						
						//Tag tag = new  Tag(strategy, strategy);
						//HibernateUtil.instance.saveTag(tag);	
					}
				}
			}
			//end of handleEvent
	    	};
	    	//end of ButListener
	    	
	    	
	    	//add the listeners for the buttons
	    	button1.addListener(SWT.Selection, butListener);
	    	button2.addListener(SWT.Selection, butListener);
	    	button3.addListener(SWT.Selection, butListener);
	    	button4.addListener(SWT.Selection, butListener);
	    	button5.addListener(SWT.Selection, butListener);
	    	
	    	//for the combo to strategy search
			ModifyListener modifyListener = new ModifyListener() {
				
				@Override
				public void modifyText(ModifyEvent arg0) {
					// TODO Auto-generated method stub
					//System.out.println("ModifyListener");
					
					//get the strategy
					String strategy = combo2.getText();
					//get list of tags of the strategy
					ArrayList<String> tagList = strategiesHash.get(strategy);
					// check that strategy correct
					if(tagList != null){
						combo3.removeAll();
						
						int len = tagList.size();						
						//add the tag to combo3 - combo with the tags
						for(int i=0; i<len; i++){
							//add new tag to the combo tags
							combo3.add(tagList.get(i));
						}
					}
				}//end modifyText
			};
			
			//add the listener to the combo
			combo2.addModifyListener(modifyListener);
	}
	//end of listenersInitialization
	
	
	
	public void updateDataOfGUI(){
		
	}

	/**
	 * the function get a string and add it to the combo 
	 * that show the strategies
	 * 
	 * @param strategy
	 */
	public void updateAddStrategyCombo(String strategy){
		addStrategyCombo.add(strategy);
	}
	
	/**
	 * get list of files name and add it to the list 
	 * 
	 * @param fileList - names of files
	 */
	public void editListOfFiles(ArrayList<String> fileList){
		int len = fileList.size();
		//empty the list
		listOfFiles.removeAll();
		
		//go over the list
		for(int i=0; i<len; i++){
			listOfFiles.add(fileList.get(i));
		}
	}
	//end editListOfFiles
}
